//
//  ScrollTitleView.m
//  WidgetDemo
//
//  Created by 航汇聚科技 on 2018/3/13.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "ScrollTitleView.h"

@interface ScrollTitleView ()
{
    NSMutableArray *_titleButtons;
    
    UIColor *_tempNormalColor;
    UIColor *_tempTouchedColor;
    CGFloat _tempHorizontalSpace;
    CGFloat _tempFirstBtnSpace;
    
    UIFont *_normalFont;
    UIFont *_touchedFont;
}

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *scrollContentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollContentViewWidth;

@end

@implementation ScrollTitleView

+ (instancetype)xib {
    return (ScrollTitleView *)[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

static int titleBtnBaseTag = 1000;
-(void)drawWithTitles:(NSArray *)titles titleFont:(UIFont *)titleFont touchedTitleFont:(UIFont *)touchedFont normalTitleColor:(UIColor *)normalColor touchedTitleColor:(UIColor *)touchedColor eachTitleHorizontalSpace:(CGFloat)horizontalSpace {
    NSArray *arr = titles;
    _titleButtons = [NSMutableArray arrayWithCapacity:arr.count];
    UIFont *fnt = titleFont;
    _tempNormalColor = normalColor;
    _tempTouchedColor = touchedColor;
    
    _normalFont = titleFont;
    _touchedFont = touchedFont;
    
    CGFloat totalWidth = 0.00;
    int i_size = 0;
    for (NSString *str in arr) {
        CGSize size = CGSizeZero;
        if (i_size == 0) {
            size = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:touchedFont,NSFontAttributeName,nil]];
        }else {
            size = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName,nil]];
        }
        
        totalWidth += size.width;
    }

    self.scrollContentViewWidth.constant = totalWidth + horizontalSpace * arr.count;
    
    int i = 0;
    CGFloat labelTotoalWidth = 5.00;
    _tempFirstBtnSpace = labelTotoalWidth;
    _tempHorizontalSpace = horizontalSpace;
    for (NSString *str in arr) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:str forState:UIControlStateNormal];
        button.titleLabel.font = fnt;
        CGSize size = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:touchedFont,NSFontAttributeName,nil]];
        if (i != 0) {
            CGSize formerSize = [[arr objectAtIndex:i-1] sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName,nil]];
            labelTotoalWidth += horizontalSpace + formerSize.width;
            [button setTitleColor:normalColor forState:UIControlStateNormal];
        }else {
            //默认选中第一个
            [button setTitleColor:touchedColor forState:UIControlStateNormal];
        }
        [button setImage:nil forState:UIControlStateNormal];
        button.frame = CGRectMake(labelTotoalWidth, self.scrollContentView.bounds.size.height/2 - size.height/2, size.width, size.height);
        button.tag = i + titleBtnBaseTag;
        [self.scrollContentView addSubview:button];
        [_titleButtons addObject:button];
        i++;
        
        [button addTarget:self action:@selector(touchItemButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}
- (void)touchItemButton:(UIButton *)sender {
    NSLog(@"%ld %@", (long)sender.tag, sender.titleLabel.text);
    
    CGFloat increasedWidth = 0.00; // 增加的宽度
    CGFloat totalWidth = 0.00;
    for (UIButton *btn in _titleButtons) {
        if (sender.tag == btn.tag) {
            [btn setTitleColor:_tempTouchedColor forState:UIControlStateNormal];
            btn.titleLabel.font = _touchedFont;
            
            CGSize size = [btn.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:_touchedFont,NSFontAttributeName,nil]];
            CGRect senderRect = sender.frame;
            increasedWidth = size.width - senderRect.size.width;
            senderRect.size = size;
            sender.frame = senderRect;
            
            totalWidth += size.width;
            
        }else {
            [btn setTitleColor:_tempNormalColor forState:UIControlStateNormal];
            btn.titleLabel.font = _normalFont;
            
            CGSize size = [btn.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:_normalFont,NSFontAttributeName,nil]];
            totalWidth += size.width;
        }
    }
    
    self.scrollContentViewWidth.constant = totalWidth + _tempHorizontalSpace * _titleButtons.count;
    
    
    CGRect screenRect = [UIScreen mainScreen].bounds;
    
    // 偏移量 = btn的最右边的X坐标 - 屏幕中心
    /*
     |-----------------------| 屏幕 screenW
                |center
     |-----------------***-------------------------|scrollContent
                       (btnX+btnW)
|---------------***-------------------------------| 偏移后
|----| offset + center = btnX + btnW (这里的btnX是相对于父视图而言的坐标系)
     */
    CGPoint offset = CGPointMake(sender.frame.origin.x + sender.frame.size.width - screenRect.size.width/2, 0);
    
    // 紧贴屏幕左边
    if (sender.frame.origin.x < screenRect.size.width/2) {
        offset = CGPointMake(0, 0);
    }
    int screenWidth = screenRect.size.width;
    
    // 紧贴屏幕右边
    int isLastBtns = sender.tag - titleBtnBaseTag >= _titleButtons.count - 3;
    if (isLastBtns) {
        offset = CGPointMake(self.scrollContentViewWidth.constant - screenWidth, 0);
    }
    [self.scrollView setContentOffset:offset animated:YES];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(showWithTitle:tag:)]) {
        [self.delegate showWithTitle:sender.titleLabel.text tag:sender.tag];
    }
}


@end
